*** Regressions using subbasin or `level' level obs instead of grid cells
*** switch macro defintions at top to change between them
*also need to rename variable "conpfaf4 to pfaf4" for subbasin to work 

** for consistency with cell-based eqns flare .5 degree cells already dropped

** macros to switch from admin1/pfaf4 analysis
/*
local level "admin1"
local level_var "admin1"
local descript "administrative unit"

local level "pfaf4"
local level_var "conpfaf4"
local descript "subbasin"
*/

capture log close
log using reg_noncell, replace
timer on 1



foreach level in pfaf4 admin1 {

use for_reg_`level'.dta, clear

if "`level'"=="pfaf4" {
drop pfaf4
rename conpfaf4 pfaf4
local descript "subbasin"
local appendix "I"
} 
else {
local descript "administrative unit"
local appendix "J"
}


gen byte esmdsi=(dsi_cat11<=3) if !missing(dsi_cat11)

gen byte extpdsi=(pdsi_cat==1) if !missing(pdsi_cat)
gen byte extsevpdsi=(pdsi_cat<=2) if !missing(pdsi_cat)
gen byte esmpdsi=(pdsi_cat<=3) if !missing(pdsi_cat)

label variable extpdsi "Extreme drought scPDSI" 
label variable esmdsi "Moderate or worse drought (DSI)" 
label variable esmpdsi "Moderate or worse drought (PDSI)"


xtset `level' year


eststo:  xtreg ihslights ib6.dsi_cat11 i.year#i.continent, fe vce(cluster `level') 
estimates store dsi_basic



**add temperature data
eststo: xtreg ihslights ib6.dsi_cat11 c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent,  fe vce(cluster `level')
estimates store dsi_withtemp



eststo: xtreg ihslights esmdsi c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent, fe vce(cluster `level')
estimates store dsi_binary


xtreg ihslights ib6.pdsi_cat i.year#i.continent, fe vce(cluster `level')
eststo pdsi_basic

eststo: xtreg ihslights ib6.pdsi_cat c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent, fe vce(cluster `level')
estimates store pdsi_withtemp

eststo: xtreg ihslights esmpdsi c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent, fe vce(cluster `level')
estimates store pdsi_binary


coefplot  (dsi_withtemp, label(DSI)) (pdsi_withtemp, label(sc-PDSI)), base drop(_cons *year* *tmp*) xline(0) levels(95) xtitle("Change in nighttime lights")  graphregion(fcolor(white)) rename(*.dsi_cat11 = .pdsi_cat)
graph export "../output/Figure`appendix'1_`level'.emf", replace

eststo clear


**# Impacts of dams: Interactions with drought dummy

* characteristics for interactions
gen nonhydro=((count_dams-count_hydro)>0)
replace rescap=rescap/10^3
replace rescaphydro=rescaphydro/10^3
*gen up1_nonhydro=((up1_count_dams-up1_hydro)>0)
*gen farupdam=((upstream_dams-up1_count_dams)>0)
gen pop=pop2000/10^3

*explicit interactions for cleaner output
* upstream variables not defined at admin unit level 
foreach var in ifdam ifhydro nonhydro pop rescap rescaphydro /* up1_ifdam up1_ifhydro up1_nonhydro farupdam */ {
	gen esmdsi_`var'=(dsi_cat11<=3)*`var'
	gen esmpdsi_`var'=(pdsi_cat<=3)*`var'
}


label var esmdsi_ifdam "Dam* drought"
label var esmdsi_ifhydro "Hydro dam* drought"
label var esmdsi_nonhydro "Nonhydro dam* drought"
label var esmdsi_pop "Pop density* drought"
label var esmdsi_rescap "Dam reservoir capacity* drought*"
label var esmdsi_rescaphydro "Hydro reservoir capacity* drought"


eststo: xtreg ihslights esmdsi esmdsi_ifdam c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent , fe vce(cluster `level')
eststo: xtreg ihslights esmdsi esmdsi_ifdam esmdsi_ifhydro c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent, fe vce(cluster `level')
eststo: xtreg ihslights esmdsi esmdsi_ifdam esmdsi_ifhydro esmdsi_pop c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent, fe vce(cluster `level')
eststo: xtreg ihslights esmdsi esmdsi_ifdam esmdsi_rescap esmdsi_ifhydro esmdsi_rescaphydro c.dev_tmp##c.dev_tmp##c.dev_tmp i.year#i.continent, fe vce(cluster `level')

esttab using "../output/table`appendix'1_dam_`level'.rtf", replace se r2(3)sca( "N_clust N `descript's") order(esmdsi esmdsi_ifdam esmdsi_rescap esmdsi_ifhydro esmdsi_rescaphydro esmdsi_pop) nomti nonote obslast label noconstant onecell star( + .10 * .05 ** .01 ) drop(*dev_tmp* *.year* _cons) /*
*/ title("Effects of dams: `descript' observations") /*
*/ addnote("Notes: Dependent variable is inverse hyperbolic sine of nighttime lights index. Standard errors in parentheses are clustered by `descript'. All models include a cubic in temperature anomaly, fixed effects for `descript', and continent*year effects.")
eststo clear
}

timer off 1
timer list 1
log close

timer clear 1 

